package com.yiren.dbaa.module.device.presenter;

import android.app.Activity;

import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.device.contract.DeviceConnectContract;
import com.yiren.dbaa.module.device.model.DeviceConnectModel;
import com.yiren.dbaa.view.toast.MessageBox;

/**
 * Created by WANGRONGXIANG
 * on 2022/6/20
 * Describe
 */
public class DeviceConnectPresenter implements DeviceConnectContract.Presenter {

    private Activity activity;
    private DeviceConnectContract.Model deviceConnectModel;

    public DeviceConnectPresenter(Activity activity) {
        this.activity = activity;
        deviceConnectModel = new DeviceConnectModel(activity);
    }

    @Override
    public void connectDevice(DeviceConnectContract.View view) {
        deviceConnectModel.startSerialConnect(new CommonCallBack() {
            @Override
            public void success(Object result) {
                deviceConnectModel.connectDevice(new DeviceConnectContract.DeviceConnectCallback() {
                    @Override
                    public void onDeviceConnectSuccess() {
                        view.deviceConnectSuccess();
                    }
                    @Override
                    public void onDeviceConnectFail() {
                        activity.runOnUiThread(() -> MessageBox.show(activity, "设备连接失败，请重试！"));
                    }

                    @Override
                    public void onDeviceNoSupportAsp() {
                    }
                });
            }
        });

    }
}
